/**
 * 手机号校验
 * @param str
 * @returns {boolean}
 */
export const isPhone = (str) => {
    const cleaned = str.replace(/\D/g, '');
    const regex = /^1[3-9]\d{9}$/;
    return regex.test(cleaned);
}

/**
 * 邮箱校验
 * @param str
 */
export const isEmail = (str) => {
    const reg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
    return reg.test(str)
}

/**
 * 身份证校验
 * @param str
 * @returns {boolean}
 */
export const isChineseID = (str) => {
    const reg = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/;
    if (!reg.test(str)) return false;

    // 校验码验证
    const weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    const checkCodes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
    let sum = 0;

    for (let i = 0; i < 17; i++) {
        sum += parseInt(str.charAt(i)) * weight[i];
    }

    const mod = sum % 11;
    const lastChar = str.charAt(17).toUpperCase();

    return lastChar === checkCodes[mod];
}

/**
 * URL校验
 * @param str
 * @returns {boolean}
 */
export const isURL = (str) => {
    const reg = /^(https?|ftp):\/\/[^\s/$.?#].\S*$/i;
    return reg.test(str);
}


